home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / xlib05.zip / XPAL.ASM < prev    next >
Assembly Source File  |  1993-08-25  |  15KB  |  554 lines

  1. ;-----------------------------------------------------------------------
  2. ; MODULE XPAL
  3. ;
  4. ; Palette functions all MODE X 256 Color resolutions
  5. ;
  6. ; Compile with Tasm.
  7. ; C callable.
  8. ;
  9. ;
  10. ; ****** XLIB - Mode X graphics library                ****************
  11. ; ******                                               ****************
  12. ; ****** Written By Themie Gouthas                     ****************
  13. ;
  14. ; egg@dstos3.dsto.gov.au
  15. ; teg@bart.dsto.gov.au
  16. ;-----------------------------------------------------------------------
  17.  
  18.  
  19. COMMENT    $
  20.  
  21.  
  22.  
  23.     All the functions in this module operate on two variations of the
  24.     pallete buffer, the raw and annotated buffers.
  25.  
  26.     All those functions ending in buff operate on the following palette
  27.     structure:
  28.  
  29.        BYTE:r0,g0,b0,r1,g1,b1,...rn,gn,bn
  30.  
  31.     No reference to the starting colour index or number of colours stored
  32.     is contained in the structure.
  33.  
  34.     All those functions ending in struc operate on the following palette
  35.     structure:
  36.  
  37.        BYTE:c,BYTE:n,BYTE:r0,g0,b0,r1,g1,b1,...rn,gn,bn
  38.  
  39.     where c is the starting colour and n is the number of colours stored
  40.  
  41.  
  42.     NOTE: previously interrupts were disabled for DAC reads/writes but
  43.       they have been left enabled in this version to allow the mouse
  44.       interrupt to be invoked.
  45.  
  46. $
  47.  
  48.  
  49.  
  50.  
  51. include xlib.inc
  52. include xpal.inc
  53.  
  54. .code
  55.  
  56.  
  57. ;----------------------------------------------------------------------
  58. ; Read DAC palette into annotated type buffer with interrupts disabled
  59. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  60. ;
  61. ; x_get_pal_struc(char far * pal, int num_colrs, int start_color)
  62. ;
  63. ; WARNING: memory for the palette buffers must all be pre-allocated
  64. ;
  65. ; Written by Themie Gouthas
  66. ;----------------------------------------------------------------------
  67. _x_get_pal_struc  proc
  68. ARG  PalBuff:dword,NumColors:word,StartColor:word
  69.      push  bp        ; Set up stack frame
  70.      mov   bp,sp
  71.      push  di
  72.      push  si
  73.      cld
  74.  
  75.      les   di,dword ptr [PalBuff]  ; Point es:di to palette buffer
  76.      mov   si,[StartColor]         ; Store the Start Colour
  77.      mov   ax,si
  78.      stosb
  79.      mov   dx,[NumColors]          ; Store the Number of Colours
  80.      mov   al,dl
  81.      stosb
  82.  
  83.      mov   cx,dx                   ; setup regs and jump
  84.      jmp   short ReadPalEntry
  85.  
  86. _x_get_pal_struc endp
  87.  
  88.  
  89.  
  90.  
  91.  
  92. ;----------------------------------------------------------------------
  93. ; Read DAC palette into raw buffer with interrupts disabled
  94. ; ie BYTE r1,g1,b1,r1,g2,b2...rn,gn,bn
  95. ;
  96. ; x_get_pal_raw(char far * pal, int num_colrs, int start_index)
  97. ;
  98. ; WARNING: memory for the palette buffers must all be pre-allocated
  99. ;
  100. ; Written by Themie Gouthas
  101. ;----------------------------------------------------------------------
  102. _x_get_pal_raw    proc
  103. ARG  PalBuff:dword,NumColors:word,StartColor:word
  104.      push  bp        ; Set up stack frame
  105.      mov   bp,sp
  106.      push  di
  107.      push  si
  108.  
  109.      les   di,dword ptr [PalBuff]  ; Point es:di to palette buffer
  110.  
  111.      mov  si,[StartColor]
  112.      mov  cx,[NumColors]
  113.  
  114. ReadPalEntry:
  115.      cld
  116.      WaitVsyncStart
  117.      mov  ax,si
  118.      mov  dx,DAC_READ_INDEX
  119.      ;cli
  120.      out  dx,al                    ; Tell DAC what colour to start reading
  121.      mov  dx,DAC_DATA
  122.  
  123.      mov  bx,cx                    ; set cx to Num Colors * 3 ( size of
  124.      shl  bx,1                     ; palette buffer)
  125.      add  cx,bx
  126.  
  127.      rep  insb                     ; read the palette enntries
  128.  
  129.      ;sti
  130.      pop  si
  131.      pop  di
  132.      pop  bp
  133.      ret
  134. _x_get_pal_raw endp
  135.  
  136.  
  137.  
  138. ;----------------------------------------------------------------------
  139. ; Write DAC palette from annotated type buffer with interrupts disabled
  140. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  141. ;
  142. ; x_put_pal_struc(char far * pal)
  143. ;
  144. ; Written by Themie Gouthas
  145. ;----------------------------------------------------------------------
  146.  
  147. _x_put_pal_struc proc
  148. ARG     CompPalBuff:dword
  149.         push    bp      ;preserve caller's stack frame
  150.     mov     bp,sp   ;point to local stack frame
  151.     push    ds
  152.     push    si
  153.     cld
  154.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  155.     lodsb               ; get the colours to skip
  156.     mov     ah,0
  157.     mov     bx,ax              ; skip colours
  158.  
  159.     lodsb               ; get the count of colours to set
  160.     mov     ah,0
  161.     mov    cx,ax              ; use it as a loop counter
  162.     jmp     short WritePalEntry
  163.  
  164. _x_put_pal_struc  endp
  165.  
  166.  
  167. ;----------------------------------------------------------------------
  168. ; Write DAC palette from annotated type buffer with interrupts disabled
  169. ; starting at a new palette index
  170. ;
  171. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  172. ;
  173. ; x_transpose_pal_struc(char far * pal, int StartColor)
  174. ;
  175. ; WARNING: memory for the palette buffers must all be pre-allocated
  176. ;
  177. ; Written by Themie Gouthas
  178. ;----------------------------------------------------------------------
  179.  
  180. _x_transpose_pal_struc proc
  181. ARG     CompPalBuff:dword,StartColor:word
  182.         push    bp      ;preserve caller's stack frame
  183.     mov     bp,sp   ;point to local stack frame
  184.     push    ds
  185.     push    si
  186.     cld
  187.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  188.     mov     bx,[StartColor]
  189.     mov     [si],bl
  190.     inc     si
  191.     lodsb               ; get the count of colours to set
  192.     mov     ah,0
  193.     mov    cx,ax              ; use it as a loop counter
  194.     jmp     short WritePalEntry
  195. _x_transpose_pal_struc endp
  196.  
  197.  
  198. ;----------------------------------------------------------------------
  199. ; Write DAC palette from raw buffer with interrupts disabled
  200. ; ie BYTE r1,g1,b1,r1,g2,b2...rn,gn,bn
  201. ;
  202. ; _x_put_pal_raw(char far * pal, int num_colrs, int start_index)
  203. ;
  204. ; Written by Themie Gouthas
  205. ;----------------------------------------------------------------------
  206. _x_put_pal_raw  proc
  207. ARG  PalBuff:dword,NumColors:word,StartColor:word
  208.         push bp        ; Set up stack frame
  209.         mov  bp,sp
  210.         push ds
  211.         push si
  212.  
  213.         mov  cx,[NumColors]      ; Number of colours to set
  214.         mov  bx,[StartColor]
  215.         lds  si,[PalBuff]        ; ds:si -> palette buffer
  216.  
  217. WritePalEntry:
  218.         or   cx,cx
  219.         jz   @@Done
  220.         ;cli
  221.         cld                      ; Make sure we're going the right way
  222.         WaitVsyncStart           ; Wait for vert sync to start
  223.                 mov  ax,bx
  224.         mov  bx,60               ; set the vsync check timer (Vsync
  225.                              ; is tested for at each bx'th entry to
  226.                      ; prevent snow 60 is otimum for 10
  227.                      ; MHz 286 or greater
  228.  
  229. @@SetLoop:
  230.         mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to start
  231.         out  dx,al               ; writing from
  232.         mov  dx,DAC_DATA
  233.  
  234.         outsb                    ; Set the red component
  235.         outsb                    ; Set the green component
  236.         outsb                    ; Set the blue component
  237.         inc  al                  ; increment the colour index
  238.         dec  bx                  ; decrement vsync test counter
  239.         js   @@test_vsync        ; ready to test for vsync again ?
  240.         loop @@SetLoop           ; No! - continue loop
  241.         jmp  short @@Done        ; All colours done
  242.  
  243. @@test_vsync:
  244.         mov     dx,INPUT_STATUS_0
  245.         push    ax               ; save current colour index
  246. @@Wait:
  247.         in      al,dx            ; wait for vsync leading edge pulse
  248.         test    al,08h
  249.         jz      @@Wait
  250.  
  251.         pop     ax               ; restore current colour index
  252.         mov     bx,60            ; reset vsync test counter
  253.         loop @@SetLoop           ; loop for next colour index
  254.  
  255. @@Done:
  256.         ;sti
  257.         pop  si
  258.         pop  ds
  259.         pop  bp
  260.         ret
  261. _x_put_pal_raw endp
  262.  
  263.  
  264.  
  265. ;----------------------------------------------------------------------
  266. ; Set the RGB setting of a vga color
  267. ;
  268. ; _x_set_rgb(unsigned char color, unsigned char R,unsigned char G,
  269. ;            unsigned char B)
  270. ;
  271. ;
  272. ; Written by Themie Gouthas
  273. ;----------------------------------------------------------------------
  274. _x_set_rgb  proc
  275. ARG  ColorIndex:byte,R:byte,G:byte,B:byte
  276.         push bp        ; Set up stack frame
  277.         mov  bp,sp
  278.  
  279.         mov  al,[ColorIndex]
  280.         mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to
  281.         out  dx,al               ; write to
  282.         mov  dx,DAC_DATA
  283.  
  284.         mov  al,[R]              ; Set the red component
  285.         out  dx,al
  286.         mov  al,[G]              ; Set the green component
  287.         out  dx,al
  288.         mov  al,[B]              ; Set the blue component
  289.         out  dx,al
  290.         pop  bp
  291.         ret
  292. _x_set_rgb endp
  293.  
  294.  
  295. ;----------------------------------------------------------------------
  296. ; Rotate annotated palette buffer entries
  297. ;
  298. ; x_rot_pal_struc(char far * pal, int direction)
  299. ;
  300. ; Direction : 0 = backward 1 = forward
  301. ;
  302. ; Written by Themie Gouthas
  303. ;----------------------------------------------------------------------
  304. _x_rot_pal_struc  proc
  305. ARG  PalBuff:dword,Direction:word
  306.     push bp        ; Set up stack frame
  307.     mov     bp,sp
  308.     push ds
  309.     push si
  310.     push di
  311.  
  312.     cld
  313.     lds     si,dword ptr [PalBuff]  ; point ds:si to Palette buffer
  314.     lodsw         ; al = colorst ot skip, ah = num colors
  315.  
  316.     xor  ch,ch    ; Set the number of palette entries to cycle in cx
  317.     mov  cl,ah    ;
  318.  
  319.     jmp  short RotatePalEntry
  320.  
  321. _x_rot_pal_struc  endp
  322.  
  323.  
  324.  
  325. ;----------------------------------------------------------------------
  326. ; Rotate raw palette buffer
  327. ;
  328. ; x_rot_pal_raw(char far * pal, int direction, int num_colrs)
  329. ;
  330. ; Direcction : 0 = backward 1 = forward
  331. ;
  332. ; Written by Themie Gouthas
  333. ;----------------------------------------------------------------------
  334. _x_rot_pal_raw    proc
  335. ARG  PalBuff:dword,Direction:word,NumColors:word
  336.     push bp        ; Set up stack frame
  337.     mov     bp,sp
  338.     push ds
  339.     push si
  340.     push di
  341.  
  342.     cld
  343.     mov     cx,[NumColors]          ; Set the number of palette entries to cycle
  344.     lds     si,dword ptr [PalBuff]  ; point ds:si to Palette buffer
  345.  
  346. RotatePalEntry:
  347.  
  348.  
  349.     mov     ax,ds                ; copy ds to es
  350.     mov     es,ax
  351.  
  352.     dec  cx
  353.     mov     bx,cx                ; Multiply cx by 3
  354.     shl     bx,1
  355.     add  cx,bx
  356.  
  357.     cmp  [Direction],0        ; are we going forward ?
  358.     jne  @@forward            ; yes - jump (colors move one position back)
  359.  
  360.     std                       ; no - set reverse direction
  361.     add  si,cx                ; set si to last byte in palette
  362.     add  si,2
  363.  
  364. @@forward:
  365.     mov     ax,si                ; copy si to di
  366.     mov     di,ax
  367.  
  368.     lodsb                     ; load first color triplet into regs
  369.     mov  dl,al
  370.     lodsb
  371.     mov  dh,al
  372.     lodsb
  373.     mov  bl,al
  374.  
  375.     rep     movsb                ; move remaining triplets direction indicated
  376.                   ; by direction flag
  377.  
  378.     mov  al,dl                ; write color triplet from regs to last position
  379.     stosb
  380.     mov  al,dh
  381.     stosb
  382.     mov  al,bl
  383.     stosb
  384.  
  385.     pop     di
  386.     pop     si
  387.     pop     ds
  388.     pop     bp
  389.     ret
  390. _x_rot_pal_raw  endp
  391.  
  392. ;----------------------------------------------------------------------
  393. ; Copy palette making intensity adjustment
  394. ; x_cpcontrast_pal_struc(char far *src_pal, char far *dest_pal, unsigned char Intensity)
  395. ;
  396. ; WARNING: memory for the palette buffers must all be pre-allocated
  397. ;
  398. ; Written by Themie Gouthas
  399. ;----------------------------------------------------------------------
  400. _x_cpcontrast_pal_struc    proc
  401. ARG  PalSrcBuff:dword,PalDestBuff:dword,Intensity:byte
  402.     push bp        ; Set up stack frame
  403.     mov     bp,sp
  404.     push ds
  405.     push si
  406.     push di
  407.  
  408.     cld
  409.     mov  bh,0ffh
  410.     sub  bh,[Intensity]
  411.     and  bh,07fh            ; Palettes are 7 bit
  412.     lds     si,dword ptr [PalSrcBuff]  ; point ds:si to Source Palette buffer
  413.     les     di,dword ptr [PalDestBuff] ; point ds:si to Source Palette buffer
  414.     lodsw                ; al = colorst ot skip, ah = num color
  415.     stosw
  416.  
  417.     xor  ch,ch    ; Set the number of palette entries to adjust
  418.     mov  cl,ah    ;
  419.  
  420.     mov  dx,0     ; flag set to 0 if all output palette entries zero
  421. @@MainLoop:
  422.     lodsw
  423.     sub  al,bh               ; adjust intensity and copy RED
  424.     jns  @@DecrementOK_R
  425.     xor  al,al
  426. @@DecrementOK_R:
  427.     sub  ah,bh               ; adjust intensity and copy GREEN
  428.     jns  @@DecrementOK_G
  429.     xor  ah,ah
  430. @@DecrementOK_G:
  431.     or   dx,ax
  432.     or   dl,ah
  433.     stosw
  434.     lodsb
  435.     sub  al,bh               ; adjust intensity and copy BLUE
  436.     jns  @@DecrementOK_B
  437.     xor  al,al
  438. @@DecrementOK_B:
  439.     or   dl,al
  440.     stosb
  441.     loop @@MainLoop
  442.  
  443.     mov  ax,dx
  444.     pop  di
  445.     pop     si
  446.     pop     ds
  447.     pop     bp
  448.     ret
  449. _x_cpcontrast_pal_struc  endp
  450.  
  451.  
  452.  
  453. ;----------------------------------------------------------------------
  454. ; Write DAC palette from annotated type buffer with specified intensity
  455. ; ie BYTE colours to skip, BYTE colours to set, r1,g1,b1,r1,g2,b2...rn,gn,bn
  456. ;
  457. ; x_put_contrast_pal_struc(char far * pal, unsigned char  intensity)
  458. ;
  459. ; Designed for fading in or out a palette without using an intermediate
  460. ; working palette buffer ! (Slow but memory efficient ... OK for small
  461. ; pal strucs}
  462. ;
  463. ; Written by Themie Gouthas
  464. ;----------------------------------------------------------------------
  465.  
  466. _x_put_contrast_pal_struc proc
  467. ARG     CompPalBuff:dword,Intensity:byte
  468.         push    bp      ;preserve caller's stack frame
  469.     mov     bp,sp   ;point to local stack frame
  470.     push    ds
  471.     push    si
  472.     push    di
  473.     cld
  474.  
  475.     mov     bh,0ffh
  476.     sub     bh,[Intensity]
  477.     and     bh,07fh            ; Palettes are 7 bit
  478.     mov     di,40              ; set the vsync check timer (Vsync
  479.                    ; is tested for at each di'th entry to
  480.                    ; prevent snow 40 is otimum for 10
  481.                    ; MHz 286 or greater)
  482.     lds     si,[CompPalBuff]   ; load the source compressed colour data
  483.     lodsb               ; get the colours to skip
  484.     mov     bl,al
  485.  
  486.     lodsb               ; get the count of colours to set
  487.     mov     ah,0
  488.     mov    cx,ax              ; use it as a loop counter
  489.     or      cx,cx
  490.     jz      @@Done
  491.  
  492.     WaitVsyncStart           ; Wait for vert sync to start
  493.  
  494. @@MainLoop:
  495.         mov  al,bl
  496.     mov  dx,DAC_WRITE_INDEX  ; Tell DAC what colour index to start
  497.     out  dx,al               ; writing from
  498.     inc  dx                  ; == mov  dx,DAC_DATA
  499.  
  500.     lodsb                    ; Load each colour component, modify for
  501.     sub  al,bh               ; intensity and write to DAC H/Ware
  502.     jns  @@DecrementOK_R
  503.     xor  al,al
  504. @@DecrementOK_R:
  505.     out  dx,al
  506.  
  507.     lodsb
  508.     sub  al,bh
  509.     jns  @@DecrementOK_G
  510.     xor  al,al
  511. @@DecrementOK_G:
  512.     out  dx,al
  513.  
  514.     lodsb
  515.     sub  al,bh
  516.     jns  @@DecrementOK_B
  517.     xor  al,al
  518. @@DecrementOK_B:
  519.     out  dx,al
  520.  
  521.     inc  bl                  ; increment color index
  522.     dec  di                  ; decrement vsync test flag
  523.     js   @@test_vsync
  524.     loop @@MainLoop
  525.     jmp  short @@Done
  526.  
  527.  
  528. @@test_vsync:
  529.     mov     dx,INPUT_STATUS_0
  530.     push    ax               ; save current colour index
  531. @@Wait:
  532.     in      al,dx            ; wait for vsync leading edge pulse
  533.     test    al,08h
  534.     jz      @@Wait
  535.  
  536.     pop     ax               ; restore current colour index
  537.     mov     di,40            ; reset vsync test counter
  538.     loop    @@MainLoop       ; loop for next colour index
  539.  
  540. @@Done:
  541.     ;sti
  542.     pop  di
  543.     pop  si
  544.     pop  ds
  545.     pop  bp
  546.     ret
  547.  
  548. _x_put_contrast_pal_struc   endp
  549.  
  550.  
  551.     end
  552.  
  553.  
  554.